<?php

	include ("../includes/connect_db.php");
	include("../includes/core.php");

	// print_r($_POST);
	extract($_POST, EXTR_SKIP);
	if (!isset($idagent)) {
		$idagent = 0;
	}

	$data["msg"] = "Error occured, please check your input";
	$data["status"] = 0;

	switch ($act) {
		case "checkin":
			if (!isset($idagent) || $idagent == '') {
				$idagent = 0;
			}
			$sql = "INSERT INTO front_office
							(nama,
							tempat_lahir,
							tanggal_lahir,
							jenis_kelamin,
							banyak_orang,
							alamat,
							pekerjaan,
							passport,
							tipe_kunjungan,
							tanggal_kedatangan,
							tanggal_keberangkatan,
							datang_dari,
							pergi_ke,
							email,
							telepon,
							checkin,
							checkout,
							idagent,
							idroom)
					VALUES ('".$nama."',
							'".$tempat_lahir."',
							'".$tanggal_lahir."',
							'".$jenis_kelamin."',
							'".$banyak_orang."',
							'".$alamat."',
							'".$pekerjaan."',
							'".$passport."',
							'".$tipe_kunjungan."',
							'".$tanggal_kedatangan."',
							'".$tanggal_keberangkatan."',
							'".$datang_dari."',
							'".$pergi_ke."',
							'".$email."',
							'".$telepon."',
							'".$checkin.":".$checkin_time."',
							'".$checkout.":".$checkout_time."',
							'".$idagent."',
							'".$idroom."')";
			// echo $sql;
	
			$query = mysql_query($sql);
			if ($query) {
				$guest_phrase = ($banyak_orang > 1) ? " and the others" : "" ;
				$guest_prefix = ($jenis_kelamin == "m") ? "Mr. " : "Ms. " ;
				$data["msg"] = $guest_prefix.$nama." has successfully been registered, please take ".$guest_prefix.$nama.$guest_phrase." to their room [PRESS ESC KEY ON THE KEY BOARD TO EXIT]";
				$data["banyak"] = $banyak_orang-1;
				$data["id"] = base64_encode(mysql_insert_id());
				$data["status"] = 1;
			} else {
				$data["msg"] = mysql_error();
			}
			break;
		case "checkout":
			// $sc = json_decode(stripslashes($sc));
			$sql = "INSERT INTO service_charge (idfront_office, nama, jumlah, harga, keterangan)";
			// echo count($sc);
			if (count($sc) > 0) {
				$sql .= " VALUES";
				$i = 1;
				foreach ($sc as $key => $value) {
					if ($i > 1) {
						$sql .= ", ";
					}
					$sql .= "('".$id."', '".$value["nama"]."', '".$value["jumlah"]."', '".$value["harga"]."', 'Additional Service Charge')";
					$i++;
				}
			}

			$data["msg"] = $sql;
			$query = mysql_query($sql);
			if ($query || (count($sc) < 1)) {
				$sql = "UPDATE front_office
						SET is_paid = '1',
							is_checkout = '1',
							payment = '".$payment."',
							checkout = '".$checkout."',
							real_paid = '".$real_paid."',
							updated_at = CURRENT_TIMESTAMP
						WHERE id = '".$id."'";
				// echo $sql;
				$data["msg2"] = $sql;
				$query = mysql_query($sql);
				if ($query) {
					$sql_fo = "SELECT * FROM front_office WHERE id = '$id'";
					$query_fo = mysql_query($sql_fo);
					$result_fo = mysql_fetch_array($query_fo);

					if ($result_fo['idagent'] != 0) {
						$data_agent = getAgent($result_fo['idagent']);

						$diff = abs(strtotime($checkout) - strtotime($result_fo['checkin']));
						$days = floor($diff / (60*60*24));
						$room_payment = $result_fo["real_paid"] * $days;
						$comission = $data_agent['komisi'] * $room_payment / 100;

						$sql_insert_agent_paid = "INSERT INTO agent_paid VALUES ('$id', '$result_fo[idagent]', '0', '$comission', null)";
						$query_insert_agent_paid = mysql_query($sql_insert_agent_paid);
					}

					$data["msg"] = "Payment has been processed successfully.";
					$data["status"] = 1;
					$data["agent"] = $sql_insert_agent_paid;
				}
			}
			break;
		case "checkout_fast":
			$sql = "INSERT INTO service_charge (idfront_office, nama, jumlah, harga, keterangan)";
			if (count($sc) > 0) {
				$sql .= " VALUES";
				$i = 1;
				foreach ($sc as $key => $value) {
					if ($i > 1) {
						$sql .= ", ";
					}
					$sql .= "('".$id."', '".$value["nama"]."', '".$value["jumlah"]."', '".$value["harga"]."', 'Additional Service Charge')";
					$i++;
				}
			}

			$query = mysql_query($sql);
			if ($query || (count($sc) < 1)) {
				$sql = "UPDATE front_office
						SET 
							is_checkout = '1',
							payment = '".$payment."',
							checkout = '".$checkout." 10:00:00',
							real_paid = '".$real_paid."',
							updated_at = CURRENT_TIMESTAMP
						WHERE id = '".$id."'";
				
				$query = mysql_query($sql);
				if ($query) {
					$sql_fo = "SELECT * FROM front_office WHERE id = '$id'";
					$query_fo = mysql_query($sql_fo);
					$result_fo = mysql_fetch_array($query_fo);

					$sql_service_charge = "UPDATE service_charge SET is_paid = 1 WHERE idfront_office = '$id'";
					$query_service_charge = mysql_query($sql_service_charge);

					// if ($result_fo['idagent'] != 0) {
					// 	$data_agent = getAgent($result_fo['idagent']);

					// 	$diff = abs(strtotime($checkout) - strtotime($result_fo['checkin']));
					// 	$days = floor($diff / (60*60*24));
					// 	$room_payment = $result_fo["real_paid"] * $days;
					// 	$comission = $data_agent['komisi'] * $result_fo["real_paid"] / 100;

					// 	$sql_insert_agent_paid = "INSERT INTO agent_paid VALUES ('$id', '$result_fo[idagent]', '0', '$comission', null)";
					// 	$query_insert_agent_paid = mysql_query($sql_insert_agent_paid);
					// }

					$data["msg"] = "Payment has been processed successfully.";
					$data["status"] = 1;
					// $data["agent"] = $sql_insert_agent_paid;
				}
			}
			break;
		case "service_charge":
			// $sc = json_decode(stripslashes($sc));
			// echo count($sc);
			if (count($sc) > 0) {
				$sql = "INSERT INTO service_charge (idfront_office, nama, jumlah, harga, keterangan)";
				$sql .= " VALUES";
				$i = 1;
				foreach ($sc as $key => $value) {
					if ($i > 1) {
						$sql .= ", ";
					}
					$sql .= "('".$id."', '".$value["nama"]."', '".$value["jumlah"]."', '".$value["harga"]."', 'Additional Service Charge')";
					$i++;
				}
				$data["msg"] = $sql;
				$query = mysql_query($sql);
				if ($query) {
					$data["msg"] = "Payment has been processed successfully.";
					$data["status"] = 1;
				}
			}
			break;
		case "guests_details":
			if (count($gd) > 0) {
				$sql = "INSERT INTO guests_details
							(nama,
							tempat_lahir,
							tanggal_lahir,
							jenis_kelamin,
							alamat,
							pekerjaan,
							passport,
							email,
							telepon,
							indeks,
							idfront_office)
					VALUES";
				$i = 1;
				foreach ($gd as $key => $value) {
					if ($i > 1) {
						$sql .= ", ";
					}
					$sql .= "('".$value["nama"]."',
							'".$value["tempat_lahir"]."',
							'".$value["tanggal_lahir"]."',
							'".$value["jenis_kelamin"]."',
							'".$value["alamat"]."',
							'".$value["pekerjaan"]."',
							'".$value["passport"]."',
							'".$value["email"]."',
							'".$value["telepon"]."',
							'".$value["indeks"]."',
							'".$id."')";
					$i++;
				}
				$data["msg"] = $sql;
				$query = mysql_query($sql);
				if ($query) {
					$data["msg"] = "Guests has been regitered successfully.";
					$data["status"] = 1;
				}
			}
			break;
		case 'get_total':
			$service_charge = 0;
			foreach ($sc as $key => $value) {
				$service_charge += ($value["jumlah"] * $value["harga"]);
			}

			$sql = "SELECT * FROM setting limit 1";
			$query = mysql_query($sql);
			$result = mysql_fetch_array($query);
			$tax_service = $result["tax"] + $result["service"];

			$checkout_date = date(  "Y-m-d h:i:s", strtotime( $tanggal." 10:14:59" ) );
			$diff = abs(strtotime($checkout_date) - strtotime($checkin));
			$days = floor($diff / (60*60*24));
			$room_total = $room_price * $days;
			$grand_total = $room_total + $service_charge;
			$grand_total += $grand_total * $tax_service / 100;
			$data["msg"] = "Get total price successfully.";
			$data["amount"] = $grand_total;
			$data["status"] = 1;
			break;
	}
	echo json_encode($data);
?>